/*
** Copyright (C) 2011 Viens Yann
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <QFile>
#include <QXmlInputSource>
#include <QXmlSimpleReader>
#include <sstream>
#include <fstream>
#include "Score.hpp"
#include "ScoreLoader.hpp"
#include "ScoreDisplayer.hpp"

Score::Score(std::string const& gameName, std::string const& fileName): _gameName(gameName), _fileName(fileName)
{
	loadFromFile();
}

void	Score::addToScore(std::string const& nickName, const int score)
{
	std::list<std::pair<std::string, int> >::iterator				itBegin = _scores.begin();
	const std::list<std::pair<std::string, int> >::iterator			itEnd = _scores.end();
	
	while (itBegin != itEnd && (*itBegin).second > score)
		++itBegin;
	if (itBegin != itEnd)
	{
		_scores.insert(itBegin, std::pair<std::string, int>(nickName, score));
		if (_scores.size() > 10)
			_scores.pop_back();
	}
	else if (_scores.size() < 10)
		_scores.push_back(std::pair<std::string, int>(nickName, score));
}

void	Score::display(QWidget *parent)
{
	if (_scores.size() == 0)
		loadFromFile();
	ScoreDisplayer	displayer(parent, _gameName, _scores);
	displayer.exec();
}

void	Score::loadFromFile()
{
  QFile				file(_fileName.data());
  QXmlInputSource	source(&file);
  ScoreLoader		loader;
  QXmlSimpleReader	reader;

  reader.setContentHandler(&loader);
  reader.parse(&source);
  _scores = loader.getScores();
}

void	Score::writeInFile()
{
	std::ofstream		fileStream(_fileName.data());
	std::stringstream	stream;

	if (fileStream)
	{
		fileStream.write("<?xml version=\"1.1\" encoding=\"UTF-8\"?>", 38);
		fileStream.write("<ranking>", 9);
		std::list<std::pair<std::string, int> >::const_iterator			itBegin = _scores.begin();
		const std::list<std::pair<std::string, int> >::const_iterator	itEnd = _scores.end();
		while (itBegin != itEnd)
		{
			fileStream.write("<position>", 10);
			fileStream.write(("<nickname>" + (*itBegin).first + "</nickname>").data(), 21 + (*itBegin).first.length());
			stream << (*itBegin).second;
			fileStream.write(("<score>" + stream.str() + "</score>").data(), 15 + stream.str().length());			
			stream.str("");
			fileStream.write("</position>", 11);
			++itBegin;
		}
		fileStream.write("</ranking>", 10);
	}
}

std::list<std::pair<std::string, int> > const&	Score::getScores() const
{
	return _scores;
}

std::pair<std::string, int> const&				Score::last() const
{
	return _scores.back();
}

bool	Score::isEmpty() const
{
	return _scores.size() == 0;
}

int		Score::getNbScore() const
{
	return _scores.size();
}